package com.frxuexi.soufang

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.adapter.Home1Adapter
import com.frxuexi.adapter.Home2Adapter
import com.frxuexi.adapter.NewHourseAdapter
import com.frxuexi.adapter.OldHourseAdapter
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.soufang.databinding.ActivityHourseBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HourseActivity : BaseActivity<ActivityHourseBinding,HourseViewModel>() {
    val home1Adapter by lazy { Home1Adapter() }
    val home2Adapter by lazy { Home2Adapter() }
    val oldHourseAdapter by lazy { OldHourseAdapter() }
    val newHourseAdapter by lazy { NewHourseAdapter() }
    override fun initData() {
        vm.hourseSuc.observe(this){
            home1Adapter.addData(it.cityMenuMap.baseMenus)
            home1Adapter.notifyDataSetChanged()

            home2Adapter.addData(it.cityMenuMap.menus)
            home2Adapter.notifyDataSetChanged()

            val entrance = it.topEntrances.get(0)
            binding.homeTv1.setText(entrance.typeName)
            binding.homeTv2.setText(entrance.title)
            binding.homeTv3.setText(entrance.desc)

            val entrance2 = it.topEntrances.get(1)
            binding.homeTv4.setText(entrance2.typeName)
            binding.homeTv5.setText(entrance2.title)
            binding.homeTv6.setText(entrance2.desc)
        }

        vm.hourseMenuSuc.observe(this){
            it.forEach {
                binding.homeTab.apply {
                    addTab(newTab().setText(it.desc))
                }
            }

        }

        vm.oldHourseSuc.observe(this){
            oldHourseAdapter.data.clear()
            oldHourseAdapter.addData(it.list)
            oldHourseAdapter.notifyDataSetChanged()
        }

        vm.newHourseSuc.observe(this){
            newHourseAdapter.data.clear()
            newHourseAdapter.addData(it.list)
            newHourseAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        if(!SPUtils.getInstance().getBoolean("isShow",false))
            initDialog()

        binding.rvHome1.apply {
            adapter=home1Adapter
            layoutManager=GridLayoutManager(this@HourseActivity,5)
        }
        binding.rvHome2.apply {
            adapter=home2Adapter
            layoutManager=GridLayoutManager(this@HourseActivity,5)
        }
        binding.rvHourse.apply {
            adapter=oldHourseAdapter
            layoutManager=LinearLayoutManager(this@HourseActivity)
        }

        home1Adapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/soufang/HoursescActivity").navigation()
        }

        oldHourseAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/soufang/HourseDetailActivity").withString("id",oldHourseAdapter.getItem(position).id).navigation()
        }

        vm.getHourseHome()
        vm.getHourseMenu()
        vm.getOldHourse()


        binding.homeTab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                Log.i("===",tab?.position.toString())
                if(tab?.position==2){
                    binding.rvHourse.adapter=newHourseAdapter
                    vm.getNewHourse()
                }else{
                    binding.rvHourse.adapter=oldHourseAdapter
                    vm.getOldHourse()
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
    }

    private fun initDialog() {

        val view = LayoutInflater.from(this).inflate(R.layout.mydialog, null)
        val dialog_content = view.findViewById<TextView>(R.id.dialog_content)
        val alertDialog = AlertDialog.Builder(this).setView(view)
            .create()
        alertDialog.show()
        view.findViewById<TextView>(R.id.dialog_agree).setOnClickListener {
            SPUtils.getInstance().put("isShow",true)
            alertDialog.dismiss()
        }
        val spannableString = SpannableString(dialog_content.text)
        val index = spannableString.indexOf("《用户协议》")
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 点击事件处理
                ARouter.getInstance().build("/soufang/UserPreActivity")
                    .withString("url","https://m.qfang.com/shenzhen/user/user-protocol.html").navigation()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.BLUE // 设置点击文本的颜色
                ds.isUnderlineText = true // 添加下划线
            }
        }

        spannableString.setSpan(
            clickableSpan,
            index, index+"《用户协议》".length, // 应用到 "World"
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )


        val index2 = spannableString.indexOf("《隐私协议》")
        val clickableSpan2 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                // 点击事件处理
                ARouter.getInstance().build("/soufang/UserPreActivity")
                    .withString("url","https://m.qfang.com/shenzhen/user/privacy-protocol.html").navigation()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.BLUE // 设置点击文本的颜色
                ds.isUnderlineText = true // 添加下划线
            }
        }

        spannableString.setSpan(
            clickableSpan2,
            index2, index2+"《隐私协议》".length, // 应用到 "World"
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        dialog_content.text=spannableString
        dialog_content.movementMethod = LinkMovementMethod.getInstance()



    }

}